	package browser.service;

	import browser.dao.ExportDataDao;
	import browser.dao.UserInfo;
	import java.io.IOException;
	import java.sql.Connection;
	import java.sql.DriverManager;
	import java.sql.SQLException;
	import javax.servlet.ServletException;
	import javax.servlet.ServletOutputStream;
	import javax.servlet.http.HttpServlet;
	import javax.servlet.http.HttpServletRequest;
	import javax.servlet.http.HttpServletResponse;

	/**
	 * Servlet that will export query data into excel
	 *
	 */
	public class ExportDataServlet extends HttpServlet {

		public void doGet(HttpServletRequest request,
				HttpServletResponse response) throws ServletException,
				IOException {

			this.doPost(request, response);
		}

		public void doPost(HttpServletRequest request,
				HttpServletResponse response) throws ServletException,
				IOException {

			String query = java.net.URLDecoder.decode(request
					.getParameter("query"), "UTF-8");
			String uid = request.getParameter("uid");
			try {
				Class.forName("oracle.jdbc.driver.OracleDriver");
			} catch (ClassNotFoundException e) {
				return;
			}

			Connection conn = null;
			UserInfo user = MyUtilities.getUserInfo(uid);
			try {
				conn = DriverManager.getConnection("jdbc:oracle:thin:"
						+ user.getUid() + "/" + user.getPassword()
						+ "@oraserver.cs.luc.edu:1521:cs");
			} catch (SQLException e1) {
				return;
			}
			if (conn == null) {
				return;
			}

			request.setCharacterEncoding("utf-8");

			response.setHeader("Pragma", "no-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			response.setContentType("application/vnd.ms-excel;charset=utf-8");

			ServletOutputStream sos = response.getOutputStream();

			ExportDataDao edd = new ExportDataDao();
			edd.outDB(conn, query, sos);
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			sos.flush();
			sos.close();
		}

	}